﻿using Ninject;
using Ninject.Modules;
using Nx.Shell.WP7;
using Nx.Shell.WP7.Modules;
using Nx.UI.Extensions;
using System.Windows;
namespace Nx.Shell.AppServices
{
    public class KernelService : IApplicationService, IApplicationLifetimeAware
    {
        public static IKernel Kernel { get; private set; }

        #region IApplicationService Members
        void IApplicationService.StartService(ApplicationServiceContext context)
        {
            var modules = new INinjectModule[] 
            { 
                new ShellModule(),
                //new TT.Module1.InitModule(),
                //new TT.Module2.InitModule()
            };

            using (var bootstrapper = new Bootstrapper(modules)
                .ExtendBy(new NxUIExtension())
                )
            {
                Kernel = bootstrapper.Run();
            }
        }

        void IApplicationService.StopService()
        {
        }
        #endregion

        #region IApplicationLifetimeAware
        void IApplicationLifetimeAware.Exited()
        {
        }

        void IApplicationLifetimeAware.Exiting()
        {
            if (Kernel != null)
            {
                Kernel.Dispose();
            }
        }

        void IApplicationLifetimeAware.Started()
        {
        }

        void IApplicationLifetimeAware.Starting()
        {
        }
        #endregion
    }
}
